package Twitim::API::HomeTimeline;

use strict;
use warnings;
use Data::Dumper;

use HTML::Entities;
use base qw( Twitim::API );

my %delay_timer;

sub _start {
    my ( $self, $output ) = @_;

    my %extra;
    if ( $self->{'max_message_id'} ) {
        $extra{'since_id'} = $self->{'max_message_id'};
    }

    $self->_getfromapi( 'statuses/home_timeline', '.json', $output, \%extra );
}

sub _last {
    my ( $self, $array_ref, $output, $api ) = @_;
    my $num = 0;

    # print "\n";

    @{$array_ref} = sort { $a->{'id'} <=> $b->{'id'}; } @{$array_ref};

    foreach my $hash_ref (@$array_ref) {

        # for track message's user name.
        # $jid = $option if ( defined $option and $option =~ /\(.*\)/ );

        my $rt = $hash_ref->{'retweeted_status'}{'text'} ? 1 : 0;
        if ($rt) {
            $hash_ref = $hash_ref->{'retweeted_status'};
        }

        my $jid       = $hash_ref->{'user'}{'screen_name'};
        my $id        = $hash_ref->{'id'};
        my $body      = $hash_ref->{'text'};
        my $time      = $self->get_unixtime( $hash_ref->{'created_at'} );
        my $source    = $hash_ref->{'source'};
        my $favorited = $hash_ref->{'favorited'};

        Twitim::NetBase->set_username( $jid, $hash_ref->{'user'}{'name'} );
        Twitim::NetBase->set_userid( $jid, $hash_ref->{'user'}{'id'} );
        Twitim::NetBase->set_icon( $jid,
            $hash_ref->{'user'}{'profile_image_url'} );
        Twitim::NetBase->set_favorites( $id, $favorited ) if $favorited;

        #print Dumper($hash_ref) . "\n";
        #
        return if ( defined $output && $output eq 'none' );    #fixme: need?

        my $my_id = Twitim::NetBase->get_my_id;

        my $ua_re = $self->{'include_web_msg'} ? '^im$' : '^(im|web)$';

        # FIXME
        if ($rt) {
            ;
        }
        elsif ((   ( $id > $self->{'max_message_id'} ) && !(
                    $jid eq $my_id
                    && ( $source =~ $ua_re || $source =~ /Twitim/ )
                )
            )
            || ( defined $output && $output eq 'Timeline' )
            )
        {
            $self->{'account_obj'}->{'max_message_id'} = $id;

            $self->{'account_obj'}->{'message_count'}++ if $jid ne $my_id;
            # print $self->{'account_obj'}->{'message_count'}, "\n";
        }
        else {
            next;
        }

        # output
        if ( !defined $output && $self->{'delay'} ) {
            my $delay = $num * $self->{'interval'} / ( $#{$array_ref} + 1 );

            #   = $num * $self->_get_interval($api) / ( $#{$array_ref} + 1 );
            # print $delay, "\n";

            $delay_timer{$id} = AnyEvent->timer(
                after => $delay,
                cb    => sub {

                    # print $self->{'interval'}, " $delay: $jid $body\n";
                    $self->{'cb'}->{'message'}->(
                        {   user_id    => $jid,
                            body       => decode_entities($body),
                            message_id => $id,
                            unixtime   => $time,
                            source     => $source,
                            favorited  => $favorited,
                            rt         => $rt,
                            output     => $output
                        }
                    );
                    delete $delay_timer{$id};
                }
            );
            $num++;
        }
        else {
            $self->{'cb'}->{'message'}->(
                {   user_id    => $jid,
                    body       => decode_entities($body),
                    message_id => $id,
                    unixtime   => $time,
                    source     => $source,
                    favorited  => $favorited,
                    rt         => $rt,
                    output     => $output
                }
            );
        }
    }

    return;
}

1;
